﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LuaInterface;

namespace Zombie_Demo.Utilities.Scripting
{
    public class Lua_Virtual_Machine
    {
        private Lua luaVM;

        public Lua_Virtual_Machine()
        {
            luaVM = new Lua();
        }

        public bool RunLuaFile(string filePath)
        {
            try
            {
                luaVM.DoFile(filePath);
                return true;
            }
            catch (Exception e)
            {
                Utilities.ErrorManager.AddError(e);
                return false;
            }
        }

        public bool RunString(string command)
        {
            try
            {
                luaVM.DoString(command);
                return true;
            }
            catch (Exception e)
            {
                Utilities.ErrorManager.AddError(e);
                return false;
            }
        }

        public bool AddMethodToLua(string refCallName, object containingObject, System.Reflection.MethodBase method)
        {
            try
            {
                luaVM.RegisterFunction(refCallName, containingObject, method);
                return true;
            }
            catch (Exception e)
            {
                Utilities.ErrorManager.AddError(e);
                return false;
            }
        }
    }
}
